import { createRouter, createWebHistory } from 'vue-router'
import LocalCache from '@/utils/cache.js'
import { firstMenu } from '@/utils/mapMenus.js'

const Layout = () => import(/* webpackChunkName: "home" */ '@/layout/index.vue')
// 不存在的页面
const NotFound = () => import(/* webpackChunkName: "notfound" */ '@/views/NotFound/NotFound.vue')
// 登录
const Login = () => import(/* webpackChunkName: "login" */ '@/views/Login/Login.vue')
// 首页
const DashBoard = () => import(/* webpackChunkName: "DashBoard" */ '@/views/DashBoard/dashboard.vue')
// 个人信息模块
const Info = () => import(/* webpackChunkName: "Info" */ '@/views/Info/Info.vue')
// 我的消息模块
const Chat = () => import(/* webpackChunkName: "Message" */ '@/views/Message/Chat')
const Notices = () => import(/* webpackChunkName: "Message" */ '@/views/Message/Notices')
const Message = () => import(/* webpackChunkName: "Message" */ '@/views/Message/Chat/component/Message.vue')
const NoticesDetail = () => import('@/views/main/admin/Notices/components/NoticesDetail.vue')
const UserNoticesDetail = () => import(/* webpackChunkName: "Message" */ '@/views/Message/Notices/components/NoticesDetail.vue')


const routes = [
  { path: "/:pathMatch(.*)", component: NotFound,meta: { title: '404' } },
  { path: '/', redirect: 'login' },
  { path: '/login', name: 'Login', component: Login, meta: { title: '用户登录' } },
  {
    path: '/home',
    name: 'Home',
    component: Layout,
    meta: { title: '首页' },
    // 公共二级路由
    children: [
      // 重定向
      // 公共部分
      { path: '/home', redirect: '/home/dashboard' },
      { path: '/home/personal', redirect: '/home/personal/info' },
      { path: '/home/message', redirect: '/home/message/chat' },
      // 管理员
      { path: '/home/users', redirect: '/home/users/admins' },
      { path: '/home/colleges', redirect: '/home/colleges/classes' },
      { path: '/home/lesson', redirect: '/home/lesson/list' },
      { path: '/home/notices', redirect: '/home/notices/publish' },
      { path: '/home/scores', redirect: '/home/scores/list' },
      // 教师
      { path: '/home/course', redirect: '/home/course/list' },
      { path: '/home/coursecase', redirect: '/home/coursecase/list' },
      { path: '/home/mystu', redirect: '/home/mystu/list' },
      { path: '/home/enter', redirect: '/home/enter/list' },
      // 学生
      { path: '/home/selection', redirect: '/home/selection/major' },
      { path: '/home/case', redirect: '/home/case/list' },
      { path: '/home/myscore', redirect: '/home/myscore/list' },
      { path: '/home/record', redirect: '/home/record/list' },
      // 首页
      { path: 'dashboard', name: 'DashBoard', component: DashBoard, meta: { title: '首页' } },
      // 个人信息模块
      { path: 'personal/info', name: 'Info', component: Info, meta: {title: '信息维护'}},
      // 我的消息模块
      { path: 'message/notices', name: 'Notices', component: Notices, meta: { title: '查看公告' }},
      {
        path: 'message/chat', name: 'Chat', component: Chat, meta: { title: '聊天会话' },
        children: [
          { path: '/home/message/chat/:id', name: 'Message', component: Message, meta: {title: '聊天会话'} }
        ]
      },
      // 在这里注册三级路由
      { path: '/home/notices/history/:id', name: 'NoticesDetail', component: NoticesDetail, meta: { title: '公告详情' } },
      { path: '/home/message/notices/:id', name: 'UserNoticesDetail', component: UserNoticesDetail, meta: {title: '公告详情'} }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 定义前置导航守卫
router.beforeEach((to, from) => {
  document.title = to.meta.title
  if (to.path !== "/login") {
    const token = LocalCache.getCache('token')
    if (!token) {
      return "/login"
    }
  }

  // // 如果路径为 /main ，就重定向到第一个页面
  // if (to.path === '/home') {
  //   return firstMenu.url
  // }
})

export default router
